﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WebManager;

namespace BaoJian.WebManager.eshop
{
    public partial class eproductedit : System.Web.UI.Page
    {
        private string ColumnID = null;
        private string oid = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            oid = HttpContext.Current.Request.QueryString["OID"];
            ColumnID = HttpContext.Current.Request.QueryString["ColumnID"];
            if (!Page.IsPostBack)
            {
                LYQ.BLL.EProduct bll = new LYQ.BLL.EProduct();
                LYQ.Model.EProduct model = bll.GetModel(new Guid(oid));

                txtSID.Text = model.SID.ToString();

                drpPName.DataSource = bll.GetList("").Tables[0].DefaultView;
                drpPName.DataBind();
                ListItem item = drpPName.Items.FindByValue(oid);
                item.Selected = true;

                Image1.ImageUrl = model.ImagePath;

                txtPrice.Text = model.Price.ToString();
                txtMarketPrice.Text = model.MarketPrice.ToString();
                txtDiscount.Text = model.Discount.ToString();

                if (model.PCatalog1 != Guid.Empty)
                {
                    item = PCatalog.Items.FindByValue(model.PCatalog1.ToString());
                    item.Selected = true;
                }

                if (model.PCatalog2 != Guid.Empty)
                {
                    item = PCatalog.Items.FindByValue(model.PCatalog2.ToString());
                    item.Selected = true;
                }

                if (model.PCatalog3 != Guid.Empty)
                {
                    item = PCatalog.Items.FindByValue(model.PCatalog3.ToString());
                    item.Selected = true;
                }

                if (model.PCatalog4 != Guid.Empty)
                {
                    item = PCatalog.Items.FindByValue(model.PCatalog4.ToString());
                    item.Selected = true;
                }
                chkAvailable.Checked = model.Available;  
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            LYQ.BLL.EProduct bll = new LYQ.BLL.EProduct();
            LYQ.Model.EProduct model = bll.GetModel(new Guid(oid));

            model.SID = Convert.ToInt32(txtSID.Text);
            model.PID = new Guid(drpPName.SelectedValue);
            model.Name = drpPName.SelectedItem.Text;
            model.ImagePath = Image1.ImageUrl;
            model.BImagePath = Image1.ImageUrl;
            model.Price = Convert.ToInt32(txtPrice.Text);
            model.MarketPrice = Convert.ToInt32(txtMarketPrice.Text);
            model.Discount = Convert.ToInt32(txtDiscount.Text);

            model.PCatalog1 = Guid.Empty;
            model.PCatalog2 = Guid.Empty;
            model.PCatalog3 = Guid.Empty;
            model.PCatalog4 = Guid.Empty;

            model.PCatalogName1 = "";
            model.PCatalogName2 = "";
            model.PCatalogName3 = "";
            model.PCatalogName4 = "";


            int num = 0;
            foreach (ListItem item in PCatalog.Items)
            {
                if (item.Selected)
                {
                    if (num == 0)
                    {
                        model.PCatalog1 = new Guid(item.Value);
                        model.PCatalogName1 = item.Text;
                        num++;
                    }
                    else if (num == 1)
                    {
                        model.PCatalog2 = new Guid(item.Value);
                        model.PCatalogName2 = item.Text;
                        num++;
                    }
                    else if (num == 2)
                    {
                        model.PCatalog3 = new Guid(item.Value);
                        model.PCatalogName3 = item.Text;
                        num++;
                    }
                    else if (num == 3)
                    {
                        model.PCatalog4 = new Guid(item.Value);
                        model.PCatalogName4 = item.Text;
                        num++;
                    }
                    else
                    {
                        Msg.Text = "超过了四个分类，请重新选择!";
                        return;
                    }
                }
            }

            model.Available = chkAvailable.Checked;
            bll.Update(model);
            string url = URLUtility.UrlBase + "/WebManager/eshop/eproductlist.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + "/WebManager/eshop/eproductedit.aspx?ColumnID=" + ColumnID;
            Page.Response.Redirect(url);
        }

        protected void drpPName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drpPName.SelectedValue != "-1")
            {
                LYQ.BLL.Product productBll = new LYQ.BLL.Product();
                LYQ.Model.Product productModel = productBll.GetModel(new Guid(drpPName.SelectedValue));

                Image1.ImageUrl = @"~\uploads\ExchangeProducts\" + productModel.SImg;
            }
            else
            {
                Image1.ImageUrl = "";
            }
        }
    }
}
